package com.haixue.android.academy.view.tiku;

import android.academy.haixue.com.haixue.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;

import java.io.IOException;
import java.io.InputStream;


public class ImageGetterFromAssets extends BaseImageGetter {

	public ImageGetterFromAssets(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
	}

	@Override
	public Drawable getDrawable(String source) {
		// TODO Auto-generated method stub

		InputStream inputStream = null;

		try {

			inputStream = context.getResources().getAssets().open(source);

			if (inputStream != null) {
				BitmapDrawable d = null;
				
				Bitmap bmp =BitmapFactory.decodeStream(inputStream);
				
				DisplayMetrics dm = context.getResources().getDisplayMetrics();
				
				float density  = dm.density;      // 屏幕密度（像素比例：0.75/1.0/1.5/2.0）
				
				bmp.setDensity(dm.densityDpi);
				
				int height = bmp.getHeight();
				
				
				d = new BitmapDrawable(bmp);
				
				if (width!=-1) {
				
					return DrawableZoomer.zoomDrawableByWidth(d, width);
					
				}
				
				int imageDisplayWidth = (int) (bmp.getWidth()*density);
				
				if (maxWidth>-1&&imageDisplayWidth>maxWidth) {
					imageDisplayWidth = maxWidth;
				}
				
				return DrawableZoomer.zoomDrawableByWidth(d,imageDisplayWidth);

				
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (source.contains("[")&&source.contains("]")) {

			Drawable drawable = null;
			
			int height=context.getResources().getDimensionPixelSize(R.dimen.tiiku_category_height);
			int width=context.getResources().getDimensionPixelSize(R.dimen.tiiku_category_width);
			
			drawable=new TextDrawable(width,height,context.getResources().getDimensionPixelSize(R.dimen.tiiku_category_text_size),source.substring(source.indexOf("[")+1, source.indexOf("]")));

			if (drawable != null) {
				drawable.setBounds(0, 0, width,
						height);
			}

			return drawable;

		}

		return null;
	}



}
